<?php

namespace MoChat\App\Home\Action;

use MoChat\App\Sales\Contract\SalesTaskEmployeeContract;
use MoChat\App\WorkDepartment\Contract\WorkDepartmentContract;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Sales\Constants\SalesTaskStatusCode;
use MoChat\App\Sales\Contract\SalesClientTaskContract;
use MoChat\App\Sales\Contract\SalesClientTaskEmployeeContract;
use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Constants\ErrorCode;

/**
 * 任务进度
 * @Controller
 */
class TaskProgress extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var SalesClientTaskContract
     */
    protected $salesClientTaskService;

    /**
     * @Inject
     * @var SalesClientTaskEmployeeContract
     */
    protected $salesClientTaskEmployeeService;

    /**
     * @Inject
     * @var SalesTaskContract
     */
    protected $salesTaskService;

    /**
     * @Inject
     * @var SalesTaskEmployeeContract
     */
    protected $salesTaskEmployeeService;
    /**
     * @Inject
     * @var WorkDepartmentContract
     */
    protected $workDepartmentService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/home/taskProgress", methods="GET")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $this->validated($data);
        $startTime = "2021-06-01 00:00:00";
        $endTime = date('Y-m-d H:i:s');
        if (!empty($data['time_enum']) && is_numeric($data['time_enum'])) {
            switch ($data['time_enum']) {
                //年
                case 1:
                    $startTime = date('Y-01-01 00:00:00');
                    $endTime = date('Y-m-d H:i:s');
                    break;
                //月
                case 2:
                    $startTime = date('Y-m-01 00:00:00');
                    $endTime = date('Y-m-d H:i:s');
                    break;
                //日
                case 3:
                    $startTime = date('Y-m-d 00:00:00');
                    $endTime = date('Y-m-d H:i:s');
                    break;
                default:
            }
        }
        $total = $this->salesTaskService->getTotalSales($user['corpIds'][0], $startTime, $endTime);
        $completeSales = $this->salesTaskEmployeeService->getTotalByCorpIdd($user['corpIds'][0], $startTime, $endTime);
        return [
            'total_sales' => $total,
            'incomplete_sales' => bcsub((string)$total, (string)$completeSales),
            'complete_sales' => $completeSales,
            'total_client' => $this->salesClientTaskService->getTotalSales($user['corpIds'][0], $startTime, $endTime),
            'complete_client' => $this->salesClientTaskEmployeeService->getTotalByCorpIdd($startTime, $endTime),
        ];
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'time_enum' => 'in:0,1,2,3',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'time_enum.in' => 'time_enum参数错误',
        ];
    }
}